package com.hong.serviceuser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hong.model.enums.DictEnum;
import com.hong.model.model.user.Patient;
import com.hong.service_cmn_client.feign.DictFeignClient;
import com.hong.serviceuser.mapper.PatientMapper;
import com.hong.serviceuser.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.*;

@Service
public class PatientSerciceImpl  extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    private PatientMapper patientMapper;
    private static int corePoolSize = Runtime.getRuntime().availableProcessors();
    private ExecutorService taskExe= new ThreadPoolExecutor(corePoolSize, corePoolSize+1, 10l, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(1000));
    //远程调用接口
    @Autowired
    private DictFeignClient dictFeignClient;
    /**
     *
     * @param userId
     * @return
     */
    @Override
    public List<Patient> findAllById(Long userId) {
        //根据userid查询所有就诊人信息列表
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);
        //通过远程调用，得到具体内容，查询数据字典
        patientList.stream().forEach(item ->{
            try {
                this.packPatient(item);
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        return patientList;
    }
    /**
     * 根据id查询就诊人信息，并进行返回，返回的是具有完整信息的对象
     * @param id
     * @return
     */
    @Override
    public Patient getPatientById(Long id) throws ExecutionException, InterruptedException {
        Patient patient = baseMapper.selectById(id);
        return this.packPatient(patient);
    }
    /**
     * patient对象里面其他参数封装，地区等
     */
    private Patient packPatient(Patient patient) throws ExecutionException, InterruptedException {
        //多线程优化
        FutureTask getCertificatesTypeTask = new FutureTask(new Callable() {
            @Override
            public Object call() throws Exception {
                String certificatesType = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
                return certificatesType;
            }
        });
        FutureTask getContactsCertificatesTypeTask = new FutureTask(new Callable() {
            @Override
            public Object call() throws Exception {
                String contactsCertificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
                return contactsCertificatesTypeString;
            }
        });
        FutureTask getProvinceTask = new FutureTask(new Callable() {
            @Override
            public Object call() throws Exception {
                String province = dictFeignClient.getName(patient.getProvinceCode());
                return province;
            }
        });
        FutureTask getCityTask = new FutureTask(new Callable() {
            @Override
            public Object call() throws Exception {
                String city = dictFeignClient.getName(patient.getCityCode());
                return city;
            }
        });
        FutureTask getDistrictTask = new FutureTask(new Callable() {
            @Override
            public Object call() throws Exception {
                String district = dictFeignClient.getName(patient.getDistrictCode());
                return district;
            }
        });
        taskExe.submit(getCertificatesTypeTask);
        taskExe.submit(getContactsCertificatesTypeTask);
        taskExe.submit(getProvinceTask);
        taskExe.submit(getCityTask);
        taskExe.submit(getDistrictTask);
        //根据证件类型编码，获取证件类型具体值
        String certificatesTypeString = (String) getCertificatesTypeTask.get();
        //联系人证件类型
        String contactsCertificatesTypeString  = (String) getContactsCertificatesTypeTask.get();
        //省
        String provinceString = (String) getProvinceTask.get();
        //市
        String cityString = (String) getCityTask.get();
        //区
        String districtString = (String) getDistrictTask.get();
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }
}
